package com.doujiang.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * 邮件服务类
 */
@Service
public class EmailService {
    
    @Autowired
    private JavaMailSender mailSender;
    
    /**
     * 发送验证码邮件
     */
    public void sendVerificationCode(String to, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3246485294@qq.com");
        message.setTo(to);
        message.setSubject("豆酱电影 - 邮箱验证码");
        message.setText("您的验证码是：" + code + "\n\n验证码有效期为5分钟，请及时使用。\n\n如果这不是您的操作，请忽略此邮件。");
        
        try {
            mailSender.send(message);
            System.out.println("验证码邮件发送成功，收件人：" + to);
        } catch (Exception e) {
            System.err.println("验证码邮件发送失败：" + e.getMessage());
            throw new RuntimeException("邮件发送失败", e);
        }
    }
    
    /**
     * 发送重置密码验证码邮件
     */
    public void sendResetPasswordCode(String to, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3246485294@qq.com");
        message.setTo(to);
        message.setSubject("豆酱电影 - 重置密码验证码");
        message.setText("您正在重置密码，验证码是：" + code + "\n\n验证码有效期为5分钟，请及时使用。\n\n如果这不是您的操作，请立即修改密码。");
        
        try {
            mailSender.send(message);
            System.out.println("重置密码验证码邮件发送成功，收件人：" + to);
        } catch (Exception e) {
            System.err.println("重置密码验证码邮件发送失败：" + e.getMessage());
            throw new RuntimeException("邮件发送失败", e);
        }
    }
} 